﻿using EasyToHelp.DBFactory;
using EasyToHelp.Entity;
using System;
using System.Collections.Generic;
using System.Text;

namespace EasyToHelp.Service
{
    /// <summary>
    /// 收货地址
    /// 2018-12-1
    /// </summary>
    public class ReceiveAdressService : IReceiveAdressService
    {
        #region 新增
        /// <summary>
        ///   新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(ReceiveAddress model)
        {
            if (model.defaultaddress == "true")
                if (Business.ReceiveAddressBll.UpDate(model) > 1)
                    return DBHelper.InsertExecute<ReceiveAddress>(model).ID;
            return 0;
        }
        #endregion 新增

        #region 更新
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(ReceiveAddress model)
        {
            if (model.defaultaddress == "true")
                if (Business.ReceiveAddressBll.UpDate(model) > 1)
                    return DBHelper.UpdateExecute<ReceiveAddress>(model);
            return 0;
        }
        #endregion 更新

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Delete(ReceiveAddress model)
        {
            return DBHelper.DeleteExecute<ReceiveAddress>(model);
        }
        #endregion 删除

        #region 获取当前用户收货地址
        /// <summary>
        /// 获取当前用户收货地址
        /// </summary>
        /// <returns></returns>
        public List<ReceiveAddress> GetList(string UsertId)
        {
            using (var db = ContextHelper.Context(ContextHelper._connString))
            {
                return db.Query<ReceiveAddress>().Where(X => X.UserId == UsertId).ToList();
            }
        }
        #endregion 获取当前用户收货地址


        #region 获取当前地址ID

        public ReceiveAddress GetModel(int ID)
        {
            using (var db = ContextHelper.Context(ContextHelper._connString))
            {
                return db.Query<ReceiveAddress>().Where(X => X.ID == ID).FirstOrDefault();
            }
        }
        #endregion 获取当前地址ID 
    }
}
